/*
 * This file is part of JContextFree.
 *
 * JContextFree is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JContextFree is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with JContextFree.  If not, see <http://www.gnu.org/licenses/>.
 */
package it.enricod.jcontextfree.gui.paint.java2d;

import it.enricod.jcontextfree.engine.model.IFigure;
import it.enricod.jcontextfree.engine.model.IFigureParamValues;
import it.enricod.jcontextfree.engine.model.IFigureParameters;
import it.enricod.jcontextfree.gui.paint.IFigurePainter;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;

import org.apache.log4j.Logger;

public class SquareFigurePainter implements IFigurePainter {

    private IFigure figure;
    private Logger logger = Logger.getLogger(SquareFigurePainter.class);

    public SquareFigurePainter(IFigure shape) {
        this.figure = shape;
    }

    public void draw(BufferedImage image) {
        Graphics2D g = (Graphics2D) image.getGraphics();

        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
                RenderingHints.VALUE_ANTIALIAS_ON);
        
        IFigureParamValues absoluteParams = figure.getParameters().getAbsolute();
        double s = absoluteParams.getScale();
        double latoX = absoluteParams.getX() - s / 2.0;
        double latoY = absoluteParams.getY() - s / 2.0;
        double y1 = figure.getFigureRegion().getP1().getY();

        AffineTransform oldTransform = g.getTransform();
        AffineTransform transform = new AffineTransform();
        // trasliamo in alto in modo che la figura sia corretta sul video
        transform.translate(latoX, image.getHeight() - latoY - s);
        // invertiamo l'angolo perche' a video le coordinate sono invertite
        transform.rotate(-figure.getParameters().getRelative().getRotation(), s / 2.0, s / 2.0);

        g.setTransform(transform);
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, new Double(s).intValue(), new Double(s).intValue());

        g.setTransform(oldTransform);
    }

    public IFigure getFigure() {
        return figure;
    }
}
